<div class="page">
	<div class="layoutBox">
        <div class="header">
        创建模块 {$info.title} ({$info.name}) 的数据表,MVC代码等
        </div>
        <div class="tabs">
            <div class="tabsHeader">
                <div class="tabsHeaderContent">
                    <ul>
                        <li class="selected">
                            <a href="#"><span><empty name="fields">创建{$info.name}表<else/>编辑{$info.name}表</empty></span></a>
                        </li>
                        <li>
                            <a href="#"><span>创建MVC</span></a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="tabsContent">
                <div>
                	<form method="post" action="__URL__/dochead/navTabId/__MODULE__" class="pageForm required-validate" onsubmit="return validateCallback(this, navTabAjaxDone)">
                    <table id="fieldsTable" class="list" width="100%" layoutH="50">
                        <thead>
                        <tr>
                            <th>字段名</th>
                            <th>中文名</th>
                            <th>备注参数</th>
                            <th>默认值</th>
                            <th>类型</th>
                            <th>长度</th>
                            <th>空值</th>
                            <th>主键</th>
                            <th>自增</th>
                            <th>索引</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <empty name="fields">
                            <tr id="tr_1">
                                <td>
                                <input  name="name[]" type="text" class="required" value="" size="10">
                                <input  name="name_old[]" type="hidden" value="">
                                </td>
                                <td><input  name="cname[]" type="text" class="required" value="" size="10"></td>
                                <td><input  name="cnote[]" type="text" value="" size="10"></td>
                                <td><input  name="default[]" type="text" value="" size="5"></td>
                                <td>
                                <select name="type[]" class="required">
                                <?php echo PrintOption('varchar','char:小字符(char),varchar:字符串(varchar),tinytext:短备注(tinytext),text:内容(text),mediumtext:长内容(mediumtext),longtext:超长内容(longtext),datetime:日期时间(datetime),tinyint:字节(tinyint),smallint:短整数(smallint),mediumint:中整数(mediumint),int:整数/时间戳(int),bigint:长整数(bigint),float:金额小数(float),double:大容量小数(double)'); ?>
                                </select>
                                </td>
                                <td><input  name="size[]" type="text" value="" size="2"></td>
                                <td><label><input  name="isnull1" type="checkbox" value="1">是</label></td>
                                <td><label><input  name="primary1" type="checkbox" value="1">是</label></td>
                                <td><label><input  name="autoinc1" type="checkbox" value="1">是</label></td>
                                <td>
                                <select name="key[]">
                                <?php echo PrintOption('',':-,Normal,Unique,Full Text'); ?>
                                </select>
                                </td>
                                <td><a href="javascript:;" onclick="addField(this)">新增</a>&nbsp;|&nbsp;<a href="javascript:;" onclick="orderField(this,-1)">上移</a>&nbsp;|&nbsp;<a href="javascript:;" onclick="orderField(this,1)">下移</a>&nbsp;|&nbsp;<a href="javascript:;" onclick="delField(this)">删除</a></td>
                            </tr>
                        <else/>
                        <volist name="fields" id="vo">
                            <tr id="tr_{$i}">
                                <td>
                                <input  name="name[]" type="text" class="required" value="{$vo.name}" size="10">
                                <input  name="name_old[]" type="hidden" value="{$vo.name}">
                                </td>
                                <td><input  name="cname[]" type="text" class="required" value="{$vo.cname}" size="10"></td>
                                <td><input  name="cnote[]" type="text" value="{$vo.cnote}" size="10"></td>
                                <td><input  name="default[]" type="text" value="{$vo.default}" size="5"></td>
                                <td>
                                <select name="type[]" class="required">
                                <?php echo PrintOption($vo['type'] ? $vo['type'] : 'varchar','char:小字符(char),varchar:字符串(varchar),tinytext:短备注(tinytext),text:内容(text),mediumtext:长内容(mediumtext),longtext:超长内容(longtext),datetime:日期时间(datetime),tinyint:字节(tinyint),smallint:短整数(smallint),mediumint:中整数(mediumint),int:整数/时间戳(int),bigint:长整数(bigint),float:金额小数(float),double:大容量小数(double)'); ?>
                                </select>
                                </td>
                                <td><input  name="size[]" type="text" value="{$vo.size|default=0}" size="2"></td>
                                <td><label><input  name="isnull{$i}" type="checkbox" value="1"<if condition="$vo['isnull']"> checked</if>>是</label></td>
                                <td><label><input  name="primary{$i}" type="checkbox" value="1"<if condition="$vo['primary']"> checked</if>>是</label></td>
                                <td><label><input  name="autoinc{$i}" type="checkbox" value="1"<if condition="$vo['autoinc']"> checked</if>>是</label></td>
                                <td>
                                <select name="key[]">
                                <?php echo PrintOption($vo['key'],':-,Normal,Unique,Full Text'); ?>
                                </select>
                                </td>
                                <td><a href="javascript:;" onclick="addField(this)">新增</a>&nbsp;|&nbsp;<a href="javascript:;" onclick="orderField(this,-1)">上移</a>&nbsp;|&nbsp;<a href="javascript:;" onclick="orderField(this,1)">下移</a>&nbsp;|&nbsp;<a href="javascript:;" onclick="delField(this)">删除</a></td>
                            </tr>
                        </volist>
                        </empty>
                        </tbody>
                    </table>
                    <div class="formBar">
                        <ul>
                            <li><div class="buttonActive"><div class="buttonContent"><button type="submit" onclick="return confirm('此方法不可逆，确定要继续吗？')">保存</button></div></div></li>
                            <li><div class="button"><div class="buttonContent"><button type="button" class="close">返回上层</button></div></div></li>
                        </ul>
                    </div>
                     <input type="hidden" name="modelname" value="{$info.name}" />
                     <input type="hidden" name="saveType" value="1" />
                     <input type="hidden" name="delnames" id="delnames" value="" />
                    </form>
                </div>
                <!-------选项卡二------->
                <div>
                    <form name="docheadForm2" id="docheadForm2" method="post" action="__URL__/dochead" class="pageForm required-validate">
                    <div class="pageFormContent" layoutH="50">
                        <div class="unit">
                            <label>应用名：</label>
                            <input name="modeltitle" id="modeltitle" type="text" class="required" value="{$info.title}" size="30" readonly="readonly">
                        </div>
                        <div class="unit">
                            <label>模块名：</label>
                            <input name="modelname" id="modelname" type="text" class="required alphanumeric" value="{$info.name}" size="30" readonly="readonly">
                        </div>
                        <div class="unit">
                            <label>系统内置模板：</label>
                            <select name="modelsystpl" id="modelsystpl" class="required">
                            <?php echo PrintOption($info['name'].'_'.str_replace(array('管理','模块','模型'),'',$info['title']),'Article_文章:文章模板,Photo_图片:图片模板,Video_视频:视频模板,Music_音乐:音乐模板'); ?>
                            </select>
                        </div>
                        <div class="unit">
                            <label>文章 - <?php echo str_replace(array('管理','模块','模型'),'',$info['title'])?> <br />表字段对应关系：</label>
                            <ul id="fieldsCompare">
                            	<li>请设置 表字段对应关系</li>
                            </ul>
                        </div>
                        <div class="unit">
                            <label>涉及文件：</label>
                            <ul id="filesInfo">
                            	<li>请点击创建MVC按钮...</li>
                            </ul>
                        </div>
                    </div>
                                            
                    <div class="formBar">
                        <ul>
                            <li><div class="buttonActive"><div class="buttonContent"><button type="submit">创建MVC</button></div></div></li>
                            <li><div class="button"><div class="buttonContent"><button type="button" class="close">返回上层</button></div></div></li>
                        </ul>
                    </div>
                     <input type="hidden" name="saveType" id="saveType" value="2" />
                    </form>
                </div>
            </div>
            <div class="tabsFooter">
                <div class="tabsFooterContent">
                </div>
            </div>
        </div>

	</div>
</div>
<script language="javascript">
//#################选项卡1{
function addField(obj){
	$tr = $(obj).parents('tr');
	$trIndex = parseInt($tr.attr('id').substr(3));
	$objTR = $tr.clone().insertAfter($tr).attr('id','tr_'+($trIndex+1));
	$objTR.find("input[name='name_old[]']").val('');
	$objTR.find("input[name^='isnull']").attr('name','isnull'+($trIndex+1));
	$objTR.find("input[name^='primary']").attr('name','primary'+($trIndex+1));
	$objTR.find("input[name^='autoinc']").attr('name','autoinc'+($trIndex+1));
	$('#fieldsTable tbody').find('tr').each(function(i){
		$(this).removeClass();
		if((i+1) % 2 == 0){ //偶数行
			$(this).addClass('trbg');
		}
	});
}
function orderField(obj,direction){
	$tr = $(obj).parents('tr');
	$trIndex = parseInt($tr.attr('id').substr(3));
	if(direction==-1){ 
		if($('#tr_'+($trIndex-1))[0]){ //如果前面有行 可以上移
			$('#tr_'+$trIndex).after($('#tr_'+($trIndex-1)));
		}else{
			//前面没有了则直接移动到最末尾
			$('#fieldsTable tbody tr:last').after($('#tr_'+$trIndex));
		}
	}else{ 
		if($('#tr_'+($trIndex+1))[0]){ //如果后面有行 可以下移
			$('#tr_'+$trIndex).before($('#tr_'+($trIndex+1)));
		}else{
			//后面没有了则直接移动到最前面
			$('#fieldsTable tbody tr:first').before($('#tr_'+$trIndex));
		}
	}
	$('#fieldsTable tbody').find('tr').each(function(i){
		$(this).attr('id','tr_'+(i+1));
		$(this).find("input[name^='isnull']").attr('name','isnull'+(i+1));
		$(this).find("input[name^='primary']").attr('name','primary'+(i+1));
		$(this).find("input[name^='autoinc']").attr('name','autoinc'+(i+1));
	});
}
function delField(obj){	
	if($('#fieldsTable tbody tr').length<2){
		 alert('至少要保留一个字段！');
		 return;
	}
	$('#delnames').val($('#delnames').val()+','+$(obj).parents('tr').find("input[name='name[]']").val());
	$(obj).parents('tr').remove();
}
//#################选项卡1}
//#################选项卡2{
function show_map(Select) {
	if(!Select) Select = docheadForm2.modelsystpl;
	if($(Select.options[Select.selectedIndex]).val()==''){
		$('#docheadForm2 #fieldsCompare').html('<li>请设置 表字段对应关系</li>');
		$('#docheadForm2 #filesInfo').html('<li>请点击创建MVC按钮...</li>');	
		return;	
	}
	$('#docheadForm2 #filesInfo').html('');
	$('#docheadForm2 #fieldsCompare').html('');
	var label = $('#docheadForm2 #fieldsCompare').siblings('label'); arr = label.html().split(' - ');
	label.html($(Select.options[Select.selectedIndex]).text()+' - '+arr[1]);
	
	$.post($(Select.form).attr('action')+'/action/selecttpl', $(Select.form).serialize(), function(data){
		if(!data.info){
			alert(data);
		}else{					
			$('#docheadForm2 #fieldsCompare').html(data.info);
		}
	},'json');
}
$('#modelsystpl').change(function(){show_map(this)});

$('#docheadForm2').submit(function() {	
	$.post($(this).attr('action'), $(this).serialize(), function(data){
		if(!data.info){
			alert(data);
		}else{			
			$('#docheadForm2 #filesInfo').html(data.info);
		}
	},'json');
	return false;
});
//全选
function selectAll(obj){
	$('#docheadForm2 #filesInfo input[type="checkbox"]').not($(obj)).attr('checked', !!$(obj).attr('checked'));	
}

//启动执行
show_map();
//#################选项卡2}
</script>